/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.components;

import DAO.Sesion;
import Model.Usuario;
import Presenter.UsuarioPresenter;
import Principals.Constantes;
import Principals.Main;
import Principals.Validadores;
import View.seguridad.Frm_CambiarContrasenhaPorOlvido;
import View.seguridad.Frm_OlvidoContrasenha;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author walter
 */
public class Login extends javax.swing.JFrame {

    /**
     * Creates new form Login
     */
    Validadores val = new Validadores();

    public Login() {
        initComponents();
        Main.usrSesion = new Sesion();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlLogin = new javax.swing.JPanel();
        lblInicioSesion = new javax.swing.JLabel();
        lblUsuario = new javax.swing.JLabel();
        lblContrasenha = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JTextField();
        lblOlvidoContrasenha = new javax.swing.JLabel();
        btnIniciar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        pwdContrasenha = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        pnlLogin.setBorder(javax.swing.BorderFactory.createTitledBorder("Inicio de sesión"));

        lblInicioSesion.setText("Ingrese su usuario y contraseña:");

        lblUsuario.setText("Usuario:");

        lblContrasenha.setText("Contraseña:");

        txtUsuario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtUsuarioKeyTyped(evt);
            }
        });

        lblOlvidoContrasenha.setText("¿Olvidó su contraseña?");
        lblOlvidoContrasenha.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblOlvidoContrasenhaMouseClicked(evt);
            }
        });

        btnIniciar.setText("Iniciar");
        btnIniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIniciarActionPerformed(evt);
            }
        });

        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.setMaximumSize(new java.awt.Dimension(80, 30));
        btnCancelar.setMinimumSize(new java.awt.Dimension(80, 30));
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        pwdContrasenha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                pwdContrasenhaKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout pnlLoginLayout = new javax.swing.GroupLayout(pnlLogin);
        pnlLogin.setLayout(pnlLoginLayout);
        pnlLoginLayout.setHorizontalGroup(
            pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlLoginLayout.createSequentialGroup()
                .addGap(136, 136, 136)
                .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(26, Short.MAX_VALUE))
            .addGroup(pnlLoginLayout.createSequentialGroup()
                .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlLoginLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblInicioSesion))
                    .addGroup(pnlLoginLayout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblContrasenha)
                            .addComponent(lblUsuario))
                        .addGap(18, 18, 18)
                        .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                            .addComponent(lblOlvidoContrasenha)
                            .addComponent(pwdContrasenha)))
                    .addGroup(pnlLoginLayout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(btnIniciar, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(42, Short.MAX_VALUE))
        );
        pnlLoginLayout.setVerticalGroup(
            pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlLoginLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlLoginLayout.createSequentialGroup()
                        .addComponent(lblInicioSesion)
                        .addGap(18, 18, 18)
                        .addComponent(lblUsuario))
                    .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblContrasenha)
                    .addComponent(pwdContrasenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(lblOlvidoContrasenha)
                .addGap(18, 18, 18)
                .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnIniciar)
                    .addComponent(btnLimpiar)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlLogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnIniciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIniciarActionPerformed
        String user = txtUsuario.getText();
        char[] pwd = pwdContrasenha.getPassword();
        String pass = "";
        for (int i = 0; i < pwd.length; i++) {
            pass += pwd[i];
        }
        
        Usuario usr = Main.usr = UsuarioPresenter.login(user, pass);
        
        if (usr != null) {
            if (usr.getEstadoSesion() == 1) {

                mostrarIndex(usr);
            } else {
                if (usr.getEstadoSesion() == 2) {
                    JOptionPane.showMessageDialog(this, "Por favor realice el cambio de contraseña para poder acceder.");
                    mostrarIndexOlvido(usr);
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "Usuario y/o contraseña incorrecta");
            limpiar();
        }
    }//GEN-LAST:event_btnIniciarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        System.exit(0);
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        limpiar();
    }//GEN-LAST:event_btnLimpiarActionPerformed

    private void lblOlvidoContrasenhaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblOlvidoContrasenhaMouseClicked
        Frm_OlvidoContrasenha frm_olvidoContrasenha = new Frm_OlvidoContrasenha(this);
        txtUsuario.setText("");
        pwdContrasenha.setText("");
        setVisible(false);

        JFrame principal = new JFrame("");
        principal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        principal.setSize(800, 600);
        principal.setResizable(false);
        principal.add(frm_olvidoContrasenha);
        principal.pack();
        principal.setVisible(true);
    }//GEN-LAST:event_lblOlvidoContrasenhaMouseClicked

    private void pwdContrasenhaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pwdContrasenhaKeyTyped
        if (pwdContrasenha.getPassword().length >= 8) {
            evt.consume();
        }
    }//GEN-LAST:event_pwdContrasenhaKeyTyped

    private void txtUsuarioKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtUsuarioKeyTyped
        val.validaMaxSize(evt, 13, txtUsuario.getText());
    }//GEN-LAST:event_txtUsuarioKeyTyped

    private void limpiar() {
        txtUsuario.setText("");
        pwdContrasenha.setText("");
    }

    private void mostrarIndex(Usuario usr) {
        setVisible(false);
        Index princ;
        try {
//            princ = new Index( Main.usr = usr );
            princ = new Index();
            princ.setVisible(true);
        } catch (Exception ex) {
        }
    }

    private void mostrarIndexOlvido(Usuario usr) {
        setVisible(false);
        IndexOlvidoContrasenha princ;
        try {
            princ = new IndexOlvidoContrasenha(new Frm_CambiarContrasenhaPorOlvido(usr));
            princ.setVisible(true);
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnIniciar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JLabel lblContrasenha;
    private javax.swing.JLabel lblInicioSesion;
    private javax.swing.JLabel lblOlvidoContrasenha;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JPanel pnlLogin;
    private javax.swing.JPasswordField pwdContrasenha;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
}
